// pages/fill/changeVolunteer/changeVolunteer.js
const app = getApp()
const wxRequest = require('../../../utils/index')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    screenHeight: app.globalData.screenHeight,
    showChoose: true,
    certificates: ['电子二级证书', '电工证书', '烹饪证书', '电子证书', '电工三级一甲证书', '烹饪证书', '电子证书', '电工证书', '烹饪证书', ],
    style: ['不限', '公办', '民办'],
    styleindex: 0,
    schoolindex: 0,
    schoolList: [],
    schoolAddressList: [],
    addressindex: -1,
    certificatesBoolean: true,
    pageNum: 1,
    pageSize: 10,
    isData: false,
    showImg: true,
    model: app.globalData.model,
    id: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    let arr = JSON.parse(options.data);
    this.setData({
      items: arr.schoolList,
      tab:arr.tab,
      sindex : arr.sindex,
      majorList:arr.majorList,
      score:arr.score,
      id:options.id
    })
    wxRequest.customRequest('common/getAreaList.do', 'GET', {}).then(res => {
      // console.log(res.data);
      if (res.code == '201') {
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
        return
      }
      that.setData({
        schoolAddressList: res.data
      })
      that.getSchoolList();
    }).catch(err => {
      wx.showToast({
        title: '请求失败',
        icon: 'error'
      })
      console.log(err);
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  clickschool(e) {
    this.setData({
      schoolindex: e.currentTarget.dataset.index
    })
  },
  clickstyle(e) {
    this.setData({
      styleindex: e.currentTarget.dataset.index
    })
  },
  clickaddress(e) {
    this.setData({
      addressindex: e.currentTarget.dataset.index
    })
  },
  reset() {
    this.setData({
      addressindex: -1,
      schoolindex: 0,
      styleindex: 0
    })
  },
  confirm(e) {
    this.setData({
      showChoose: true,
      schoolList: [],
      pageNum: 1
    })
    this.getSchoolList();
  },
  clickchoose(e) {
    let that = this;
    that.setData({
      showChoose: e.detail.showChoose,
    })
    // console.error(e)
  },
  close(e) {
    this.setData({
      showChoose: true
    })
  },
  lower() {
    let isData = this.data.isData;
    if (isData) {
      return;
    }
    this.getSchoolList();
  },
  search(e) {
    // console.log('222', e);
    let schoolName = e.detail.searchVal;
    this.setData({
      schoolList: [],
      pageNum: 1
    })
    this.getSchoolList(schoolName)
  },
  //查询院校列表
  getSchoolList(schoolName) {
    let that = this;
    let data = {
      schoolList:that.data.items,
      score:that.data.score
    };
    let addressindex = this.data.addressindex;
    let styleindex = this.data.styleindex;
    let style = this.data.style;
    let schoolAddressList = this.data.schoolAddressList;
    if (schoolAddressList.length > 0 && addressindex > -1) {
      data.schoolAddress = schoolAddressList[addressindex].value;
    }
    if (styleindex > 0) {
      data.schoolNature = style[styleindex];
    }
    if (styleindex > 0) {
      data.schoolNature = style[styleindex];
    }
    if (schoolName) {
      data.schoolName = schoolName;
    }
    data.pageSize = this.data.pageSize;
    data.pageNum = this.data.pageNum;
    data.id = this.data.id;
    wxRequest.request('school/getChangeSchoolList.do', data).then(res => {
      if (res.code == 200) {
        let schoolList = that.data.schoolList;
        let list = schoolList.concat(res.data.result);
        let isData = false;
        if (res.data.result.length < res.data.pageSize || res.data.pageSize * res.data.pageNum >= res.data.total) {
          isData = true;
        }
        // console.error(res.data)
        that.setData({
          schoolList: list,
          pageNum: data.pageNum + 1,
          isData: isData
        })
      }
    }).catch(err => {
      console.log(err);
    })
  },
  // toSchoolInfo(e) {
  //   let id = e.currentTarget.dataset.schoolid;
  //   wx.navigateTo({
  //     url: '/pages/schoolinfo/schoolinfo?id=' + id,
  //   })
  // },
  // 选择院校（单选）
  radioChange(e) {
    console.log(e);
    this.setData({
      id: e.detail.value
    })
  },
  jixu(e) {
    let that = this;
    let data = {
      schoolId:that.data.id,
      majorList: that.data.majorList,
      tab: that.data.tab
    }
    wxRequest.request('volunteer/updateVolunteerSchool.do', data).then(res => {
      if (res.code == 200) {
        let tempPage = getCurrentPages(); // 当前页变量
        let prevPage = tempPage[tempPage.length - 2]; // 上一页变量
        let item = res.data;
        wx.showModal({
          confirmText: "确定",
          cancelText: "取消",
          content: '是否确定更改院校',
          success(res) {
            if (res.confirm) {
              let data = that.data;
              data.value = item;
              prevPage.changeSchool(data);
              wx.navigateBack({
                delta: 1,
              })
            }
          }
        })
      }
      
    }).catch(err => {
      console.log(err);
    })
  }
})